class Node:
    def __init__(self):
        self.left = None
        self.right = None

class Solution:
    def maximumStrongPairXor(self, nums) -> int:
        tree = Node()
        for num in nums:
            node = tree
            for i in range(21, -1, -1):
                if (num >> i) & 1:
                    if node.right:
                        node = node.right
                    else:
                        node.right = Node()
                        node = node.right
                else:
                    if node.left:
                        node = node.left
                    else:
                        node.left = Node()
                        node = node.left
        self.max = 0
        def dfs(node, num, flag, index, now_num):
            if index == -1:
                self.max = max(self.max, num ^ now_num)
            else:
                if flag == 2:
                    if (num >> index) & 1:
                        if node.left:
                            next_num = now_num
                            dfs(node.left, num, 2, index - 1, next_num)
                        else:
                            next_num = now_num | (1 << index)
                            dfs(node.right, num, 2, index - 1, next_num)
                    else:
                        if node.right:
                            next_num = now_num | (1 << index)
                            dfs(node.right, num, 2, index - 1, next_num)
                        else:
                            next_num = now_num
                            dfs(node.left, num, 2, index - 1, next_num)
                if flag == 0:
                    if index > 0:
                        if (num >> (index - 1)) & 1:
                            if (num >> index) & 1:
                                if node.left:
                                    next_num = now_num
                                    dfs(node.left, num, 2, index - 1, next_num)
                                else:
                                    next_num = now_num | (1 << index)
                                    dfs(node.right, num, 0, index - 1, next_num)
                            else:
                                if node.left:
                                    next_num = now_num
                                    dfs(node.left, num, 2, index - 1, next_num)
                                if node.right:
                                    next_num = now_num | (1 << index)
                                    dfs(node.right, num, 0, index - 1, next_num)
                        else:
                            if node.left:
                                next_num = now_num
                                dfs(node.left, num, 0, index - 1, next_num)
                    else:
                        if node.left:
                            next_num = now_num
                            dfs(node.left, num, 2, index - 1, next_num)
                if flag == 1:
                    if (num >> index) & 1:
                        if node.right:
                            next_num = now_num | (1 << index)
                            dfs(node.right, num, 1, index - 1, next_num)
                    else:
                        if node.right:
                            next_num = now_num | (1 << index)
                            dfs(node.right, num, 2, index - 1, next_num)
                        else:
                            next_num = now_num
                            dfs(node.left, num, 0, index - 1, next_num)
        for num in nums:
            index = len(bin(num)[2:])
            node = tree
            for i in range(21, index, -1):
                node = node.left
            
            if node.right:
                dfs(node.right, num, 0, index - 1, 1 << index)
            dfs(node.left, num, 1, index - 1, 0)
        return self.max

data = Solution()
nums = [1,2,3,4,5]
print(data.maximumStrongPairXor(nums))
nums = [10, 100]
print(data.maximumStrongPairXor(nums))




        